#!/usr/bin/env python
# -*- coding: utf-8 -*-
#----------------------------------------------------------------------------#
# edictLookup.py
# Lars Yencken <lars.yencken@gmail.com>
# vim: ts=4 sw=4 sts=4 et tw=78:
# Sat Dec  9 14:05:10 JST 2006
#
#----------------------------------------------------------------------------#

import os, sys, optparse, codecs
import edict

#----------------------------------------------------------------------------#
# PUBLIC
#----------------------------------------------------------------------------#

def lookupWord(word, dictionary='je_edict'):
    """ Looks up the given word in an edict-style dictionary.
    """
    dictObj = edict.getDictionary(dictionary)

    if word not in dictObj:
        print >> sys.stderr, "No matches for %s" % word
    else:
        entry = dictObj[word]
        print '%12s: %s' % ('Reading', entry.reading)
        print '%12s: %s' % ('Meaning', entry.meaning)

    return

#----------------------------------------------------------------------------#
# PRIVATE
#----------------------------------------------------------------------------#

#----------------------------------------------------------------------------#
# MODULE EPILOGUE
#----------------------------------------------------------------------------#

def _createOptionParser():
    """ Creates an option parser instance to handle command-line options.
    """
    usage = \
"""%prog [options] kanji

Looks up the given Japanese word in the specified dictionary, defaulting to
edict."""

    parser = optparse.OptionParser(usage)

    parser.add_option('--debug', action='store_true', dest='debug',
            default=False, help='Enables debugging mode [False]')

    parser.add_option('-d', '--dictionary', action='store', dest='dictionary',
            default='je_edict',
            help='The dictionary to use [je_edict]')

    return parser

#----------------------------------------------------------------------------#

def main(argv):
    """ The main method for this module.
    """
    parser = _createOptionParser()
    (options, args) = parser.parse_args(argv)

    try:
        [word] = args
    except:
        parser.print_help()
        sys.exit(1)

    if not options.debug:
        # we don't want psyco in debugging mode, since it merges together
        # stack frames
        try:
            import psyco
            psyco.profile()
        except:
            pass

    # execute new code here
    lookupWord(word, options.dictionary)
    
    return

#----------------------------------------------------------------------------#

if __name__ == '__main__':
    main(sys.argv[1:])

#----------------------------------------------------------------------------#
  
# vim: ts=4 sw=4 sts=4 et tw=78:

